import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lib/utils/DpUtils.dart';
import 'package:flutter_lib/utils/FileUtils.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/AppBarTitleWidget.dart';
import 'package:flutter_my_web/widget/BlueMaterialButton.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:path/path.dart' as path;
class ReNamePage extends StatefulWidget {
  const ReNamePage({Key? key}) : super(key: key);

  @override
  State<ReNamePage> createState() => _ReNamePageState();
}
class _ReNameBean{
  String oldName="";
  String tryNewName="";
  String path="";

  _ReNameBean( this.path,this.oldName, this.tryNewName,);
}

class _ReNamePageState extends State<ReNamePage> {
  var directoryPathStr = "";
  List<_ReNameBean> beans = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: AppBarTitleWidget("批量修改文件名"),
      ),
      body: Column(
        children: [
          SimpleText("规则，_分割，保留第一和最后2个"),
          Wrap(children: [
            BlueMaterialButton("选择文件夹", () async {
              var directoryPath = await FilePicker.platform.getDirectoryPath();
              LogUtils.e("directoryPath:${directoryPath}");
              if (directoryPath == null) {
                showSnackBar("没有选择文件夹");
                return;
              }
              directoryPathStr = directoryPath;
              var directory = Directory(directoryPath);
              var list = await directory.list().toList();
              beans = [];
              beans = list
              .where((element) => element.statSync().type==FileSystemEntityType.file)
                  .map((event) => _ReNameBean(event.path, FileUtils.getFileName(File(event.path)), ""))
                  .toList();
              setState(() {});
            }),
            BlueMaterialButton("尝试修改", () {
              for(var i=0;i<beans.length;i++){
                var bean = beans[i];
                var split = bean.oldName.toString().split("_");
                var str = "${split[0]}_${split[split.length-2]}_${split[split.length-1]}";
                bean.tryNewName = str;
              }
              setState(() {});
            }),
            BlueMaterialButton("确认修改文件名", () async {
              for(var i=0;i<beans.length;i++){
                var bean = beans[i];
                var newNamePath =  path.join(File(bean.path).parent.path,"news",bean.tryNewName);
                var newNameDir =  path.join(File(bean.path).parent.path,"news");
                if(Directory(newNameDir).existsSync()==false){
                  Directory(newNameDir).create();
                }
                print("newNamePath:${newNamePath}");
                await File(bean.path).copy(newNamePath);
              }
              showSnackBar("修改成功");
              setState(() {});
            }),
          ],),

          SimpleText(
            "选择目录：${directoryPathStr}",
            fontSize: 16.sp,
          ),
          Expanded(
            child:  Row(
              children: [
                Expanded(
                    child: ListView(
                      shrinkWrap: true,
                      children: beans
                          .map((e) => _ItemWidget("${e.oldName}"))
                          .toList(),
                    )),
                Expanded(
                    child: ListView(
                      shrinkWrap: true,
                      children: beans
                          .map((e) => _ItemWidget("${e.tryNewName}"))
                          .toList(),
                    )),
              ],
            ),
          )
        ],
      ),
    );
  }
}

class _ItemWidget extends StatelessWidget {
  String name;

  _ItemWidget(this.name);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SimpleText(
        name,
      ),
      padding: EdgeInsets.symmetric(vertical: 10),
    );
  }
}
